#include "Animation.h"
namespace Framework
{
	Animation::Animation(int NumWidth,int NumHeight,float timePerImage,CSprite *s)
	{
		m_iNumWidth=NumWidth;
		m_iNumHeight=NumHeight;
		m_fTimePerImage=timePerImage;
		m_iFrameWidth=s->GetWidth()/m_iNumWidth;
		m_iFrameHeight=s->GetHeight()/m_iNumHeight;
		m_Rect.top=m_Rect.left=0;
		m_Rect.right=m_iFrameWidth;
		m_Rect.bottom=m_iFrameHeight;
		m_iIndex=0;		
		m_fWait=0;
	}
	void Animation::Update(int FirstIndex,int LastIndex,float TPF)
	{
		if (FirstIndex<0||LastIndex>m_iNumHeight*m_iNumWidth)
		{
			return;
		}
		m_fWait+=TPF;
		if (m_fWait>=m_fTimePerImage)
		{
			if (m_iIndex<LastIndex)
			{
				m_iIndex++;
			}
			else
				m_iIndex=FirstIndex;
			m_fWait=0;
			m_Rect.top=(m_iIndex/m_iNumWidth)*m_iFrameHeight;
			m_Rect.left=(m_iIndex%m_iNumWidth)*m_iFrameWidth;
			m_Rect.bottom=m_Rect.top+m_iFrameHeight;
			m_Rect.right=m_Rect.left+m_iFrameWidth;
		}
	}
}